package site

import org.springframework.dao.DataIntegrityViolationException
import grails.plugins.springsecurity.Secured



class ClientesController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    @Secured(['ROLE_ADMIN', 'IS_AUTHENTICATED_FULLY'])
    def index() {
        redirect(action: "list", params: params)
    }
    
    @Secured(['ROLE_ADMIN', 'IS_AUTHENTICATED_FULLY'])
    def list() {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [clientesInstanceList: Clientes.list(params), clientesInstanceTotal: Clientes.count()]
    }

    @Secured(['ROLE_ADMIN', 'IS_AUTHENTICATED_FULLY'])
    def create() {
        [clientesInstance: new Clientes(params)]
    }

    @Secured(['ROLE_ADMIN', 'IS_AUTHENTICATED_FULLY'])
    def save() {
        def clientesInstance = new Clientes(params)
        if (!clientesInstance.save(flush: true)) {
            render(view: "create", model: [clientesInstance: clientesInstance])
            return
        }

        flash.message = message(code: 'default.created.message', args: [message(code: 'clientes.label', default: 'Clientes'), clientesInstance.id])
        redirect(action: "show", id: clientesInstance.id)
    }

    @Secured(['ROLE_ADMIN', 'IS_AUTHENTICATED_FULLY'])
    def show() {
        def clientesInstance = Clientes.get(params.id)
        if (!clientesInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'clientes.label', default: 'Clientes'), params.id])
            redirect(action: "list")
            return
        }

        [clientesInstance: clientesInstance]
    }

    @Secured(['ROLE_ADMIN', 'IS_AUTHENTICATED_FULLY'])
    def edit() {
        def clientesInstance = Clientes.get(params.id)
        if (!clientesInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'clientes.label', default: 'Clientes'), params.id])
            redirect(action: "list")
            return
        }

        [clientesInstance: clientesInstance]
    }

    @Secured(['ROLE_ADMIN', 'IS_AUTHENTICATED_FULLY'])
    def update() {
        def clientesInstance = Clientes.get(params.id)
        if (!clientesInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'clientes.label', default: 'Clientes'), params.id])
            redirect(action: "list")
            return
        }

        if (params.version) {
            def version = params.version.toLong()
            if (clientesInstance.version > version) {
                clientesInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                    [message(code: 'clientes.label', default: 'Clientes')] as Object[],
                          "Another user has updated this Clientes while you were editing")
                render(view: "edit", model: [clientesInstance: clientesInstance])
                return
            }
        }

        clientesInstance.properties = params

        if (!clientesInstance.save(flush: true)) {
            render(view: "edit", model: [clientesInstance: clientesInstance])
            return
        }

        flash.message = message(code: 'default.updated.message', args: [message(code: 'clientes.label', default: 'Clientes'), clientesInstance.id])
        redirect(action: "show", id: clientesInstance.id)
    }

    @Secured(['ROLE_ADMIN', 'IS_AUTHENTICATED_FULLY'])
    def delete() {
        def clientesInstance = Clientes.get(params.id)
        if (!clientesInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'clientes.label', default: 'Clientes'), params.id])
            redirect(action: "list")
            return
        }

        try {
            clientesInstance.delete(flush: true)
            flash.message = message(code: 'default.deleted.message', args: [message(code: 'clientes.label', default: 'Clientes'), params.id])
            redirect(action: "list")
        }
        catch (DataIntegrityViolationException e) {
            flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'clientes.label', default: 'Clientes'), params.id])
            redirect(action: "show", id: params.id)
        }
    }
    
    def view = {
        def banner = Clientes.get( params.id )
        if (banner) {
            response.setContentLength(banner.imagem.size())
            response.setHeader('filename', banner.link)
            OutputStream out = response.outputStream
            out.write(banner.imagem)
            out.close()
        }
        else {
            response.sendError(404)
        }
    }
}
